﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.Code;
using System.Drawing;
using Telerik.Web.UI;
using WebApplication.Controls.Management.Company;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.ServiceLayer.EntityManagement;

namespace WebApplication.Restricted.Management.Company
{
	public partial class AddressesManagement : EntityPage
	{
		protected void AddressesGrid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedAddressId;
			if (!int.TryParse(editedItem["Id"].Text, out editedAddressId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			AddressEditControl addressControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as AddressEditControl;
			if (addressControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			DTOAddress dtoAddress = addressControl.EditedAddress;
			dtoAddress.Id = editedAddressId;
			AddressManager addressManager = new AddressManager(ModelEntities);
			try
			{
				addressManager.UpdateAddress(dtoAddress);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void AddressesGrid_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			AddressEditControl addressControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as AddressEditControl;
			if (addressControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			AddressManager addressManager = new AddressManager(ModelEntities);
			try
			{
				addressManager.AddAddress(addressControl.EditedAddress);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void AddressesGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedAddressId;
			if (!int.TryParse(editedItem["Id"].Text, out editedAddressId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			AddressManager addressManager = new AddressManager(ModelEntities);
			try
			{
				addressManager.DeleteAddress(editedAddressId);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		private void AddErrorMessage(string errorMessage)
		{
			Label errorLabel = new Label();
			errorLabel.Text = errorMessage;
			errorLabel.ForeColor = Color.Red;

			AddressesGrid.Controls.Add(errorLabel);
		}
	}
}
